package com.hongshu.web.service.sys.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hongshu.common.core.constant.Constantss;
import com.hongshu.common.core.domain.NoteSearchVo;
import com.hongshu.common.core.domain.R;
import com.hongshu.common.core.enums.EStatus;
import com.hongshu.common.core.global.SysConf;
import com.hongshu.system.api.RemoteChatService;
import com.hongshu.system.api.RemoteIdleService;
import com.hongshu.web.domain.entity.DailyStatistics;
import com.hongshu.web.domain.vo.DailyNewDataVO;
import com.hongshu.web.domain.vo.UserLocationVO;
import com.hongshu.web.mapper.web.WebUserMapper;
import com.hongshu.web.service.sys.*;
import com.hongshu.web.service.web.IWebUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 首页RestApi 业务层处理
 *
 * @author hongshu
 */
@Service
public class SysDashboardServiceImpl implements ISysDashboardService {

    @Autowired
    private IWebUserService userService;
    @Autowired
    private ISysNoteService noteService;
    @Autowired
    private RemoteIdleService remoteIdleService;
    @Autowired
    private RemoteChatService remoteChatService;
    @Autowired
    private ISysCommentService commentService;
    @Autowired
    private ISysVisitService visitService;
    @Autowired
    private ISysMemberService memberService;
    @Autowired
    private WebUserMapper userMapper;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public Map<String, Object> getInitData() {
        Map<String, Object> map = new HashMap<>(Constantss.NUM_FOUR);
        map.put(SysConf.VISIT_COUNT, visitService.getWebVisitCount());
        map.put(SysConf.USER_COUNT, memberService.getMemberCount(0));
        map.put(SysConf.BLOG_COUNT, noteService.getNoteCount(EStatus.ENABLE));
        map.put(SysConf.COMMENT_COUNT, commentService.getCommentCount(EStatus.ENABLE));
        map.put(SysConf.PRODUCT_COUNT, remoteIdleService.getProductCount().getData());
        map.put(SysConf.GPT_MODEL_COUNT, remoteChatService.getGptModelCount().getData());
        return map;
    }

    @Override
    public Map<String, Object> getVisitByWeek() {
        return visitService.getVisitByWeek();
    }

    @Override
    public List<Map<String, Object>> getNoteCountByType() {
        return noteService.getNoteCountByType();
    }

    @Override
    public List<Map<String, Object>> getProductCountByType() {
        R<?> r = remoteIdleService.getProductCountByType();
        Object data = r.getData();
        if (data == null) {
            return Collections.emptyList();
        }
        // 用ObjectMapper安全转换
        return objectMapper.convertValue(
                data, new TypeReference<List<Map<String, Object>>>() {
                }
        );
    }

    @Override
    public Map<String, Object> getOrderDataByWeek() {
        R<?> r = remoteIdleService.getOrderDataByWeek();
        Object data = r.getData();
        if (data == null) {
            return Collections.emptyMap();
        }
        // 类型安全转换
        return objectMapper.convertValue(data, new TypeReference<Map<String, Object>>() {
        });
    }

    @Override
    public DailyNewDataVO getDailyNewData() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 获取7天前的日期
        LocalDate thirtyDaysAgo = today.minusDays(7);
        try {
            // 获取今日新增数据
            int todayNewUsers = userMapper.getTodayNewUserCount();
            // 获取近7天的趋势数据
            List<DailyStatistics> statistics = userMapper.getStatisticsByDateRange(
                    thirtyDaysAgo, today);
            return this.buildDailyNewDataVO(todayNewUsers, statistics);
        } catch (Exception e) {
            throw new RuntimeException("获取统计数据失败", e);
        }
    }

    private DailyNewDataVO buildDailyNewDataVO(int todayNewUsers, List<DailyStatistics> statistics) {
        List<String> dates = new ArrayList<>();
        List<Integer> userCounts = new ArrayList<>();
        List<Integer> noteCounts = new ArrayList<>();
        List<Integer> productCounts = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        for (DailyStatistics stat : statistics) {
            dates.add(formatter.format(stat.getDate().toLocalDate()));
            userCounts.add(stat.getUserCount());
            noteCounts.add(stat.getNoteCount());
            productCounts.add(stat.getProductCount());
        }

        // 今日新增笔记、闲置，取最后一天的数量减去前一天的数量（或直接用今日数据，看你的业务）
        int todayNewNotes = statistics.isEmpty() ? 0 : statistics.get(statistics.size() - 1).getNoteCount();
        int todayNewProducts = statistics.isEmpty() ? 0 : statistics.get(statistics.size() - 1).getProductCount();

        return DailyNewDataVO.builder()
                .todayNewUsers(todayNewUsers)
                .todayNewNotes(todayNewNotes)
                .todayNewProducts(todayNewProducts)
                .dates(dates)
                .users(userCounts)
                .notes(noteCounts)
                .products(productCounts)
                .build();
    }


    @Override
    public List<Map<String, Object>> getNoteCountByCategory() {
        return noteService.getNoteCountByCategory();
    }

    @Override
    public List<Map<String, Object>> getProductCountByCategory() {
        R<?> r = remoteIdleService.getProductCountByCategory();
        Object data = r.getData();
        if (data == null) {
            return Collections.emptyList();
        }
        // 用ObjectMapper安全转换
        return objectMapper.convertValue(
                data, new TypeReference<List<Map<String, Object>>>() {
                }
        );
    }

    @Override
    public Map<String, Object> getNoteContributeCount() {
        Map<String, Object> resultMap = new HashMap<>();

        // 查询笔记贡献度
        Map<String, Object> noteMap = noteService.getNoteContributeCount();
        // 查询商品贡献度
        Object data = remoteIdleService.getProductContributeCount().getData();

        // 组装返回结果
        resultMap.put("contributeDate", noteMap.get("contributeDate"));
        resultMap.put("blogContributeCount", noteMap.get("blogContributeCount"));

        if (data == null) {
            resultMap.put("goodsContributeCount", Collections.emptyList());
        } else {
            // 用ObjectMapper安全转换
            Map<String, Object> productMap = objectMapper.convertValue(data, new TypeReference<Map<String, Object>>() {
            });
            resultMap.put("goodsContributeCount", productMap.get("goodsContributeCount"));
        }
        return resultMap;
    }

    @Override
    public List<NoteSearchVo> getHotNote(String noteType) {
        return noteService.getHotNote(noteType);
    }

    @Override
    public List<UserLocationVO> getUserLocations() {
        return userService.getAllUserLocations();
    }
}
